
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>访问者模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../mementomode/" />
    
    
    <link rel="prev" href="../iteratormode/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../../../../">
            
                <a href="../../../../../../">
            
                    
                    说明
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../../../../">
            
                <a href="../../../../../">
            
                    
                    设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" >
            
                <span>
            
                    
                    创建型模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1.1" >
            
                <span>
            
                    
                    简单工厂模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.2" >
            
                <span>
            
                    
                    工厂方法模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.3" data-path="../../createdmodel/abstractfactorymode/">
            
                <a href="../../createdmodel/abstractfactorymode/">
            
                    
                    抽象工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.4" data-path="../../createdmodel/bulidermode/">
            
                <a href="../../createdmodel/bulidermode/">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.5" data-path="../../createdmodel/prototypemode/">
            
                <a href="../../createdmodel/prototypemode/">
            
                    
                    原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.6" data-path="../../createdmodel/singletonmode/">
            
                <a href="../../createdmodel/singletonmode/">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../../structuredmodel/">
            
                <a href="../../structuredmodel/">
            
                    
                    结构型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.2.1" data-path="../../structuredmodel/proxymode/">
            
                <a href="../../structuredmodel/proxymode/">
            
                    
                    代理模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.2" data-path="../../structuredmodel/adaptermode/">
            
                <a href="../../structuredmodel/adaptermode/">
            
                    
                    适配器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.3" data-path="../../structuredmodel/bridgemode/">
            
                <a href="../../structuredmodel/bridgemode/">
            
                    
                    桥接模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.4" data-path="../../structuredmodel/decoratormode/">
            
                <a href="../../structuredmodel/decoratormode/">
            
                    
                    装饰模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.5" data-path="../../structuredmodel/facademode/">
            
                <a href="../../structuredmodel/facademode/">
            
                    
                    外观模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.6" data-path="../../structuredmodel/flyweightmode/">
            
                <a href="../../structuredmodel/flyweightmode/">
            
                    
                    享元模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.7" data-path="../../structuredmodel/compositemode/">
            
                <a href="../../structuredmodel/compositemode/">
            
                    
                    组合模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../">
            
                <a href="../">
            
                    
                    行为型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.3.1" data-path="../templatemethodmode/">
            
                <a href="../templatemethodmode/">
            
                    
                    模板方法模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.2" data-path="../strategymode/">
            
                <a href="../strategymode/">
            
                    
                    策略模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.3" data-path="../commandmode/">
            
                <a href="../commandmode/">
            
                    
                    命令模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.4" data-path="../chainofresponsibility/">
            
                <a href="../chainofresponsibility/">
            
                    
                    责任链模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.5" data-path="../statemode/">
            
                <a href="../statemode/">
            
                    
                    状态模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.6" data-path="../observermode/">
            
                <a href="../observermode/">
            
                    
                    观察者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.7" data-path="../mediatormode/">
            
                <a href="../mediatormode/">
            
                    
                    中介者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.8" data-path="../iteratormode/">
            
                <a href="../iteratormode/">
            
                    
                    迭代器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.3.9" data-path="./">
            
                <a href="./">
            
                    
                    访问者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.10" data-path="../mementomode/">
            
                <a href="../mementomode/">
            
                    
                    备忘录模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.11" data-path="../interpretermode/">
            
                <a href="../interpretermode/">
            
                    
                    解释器模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../../../../../Nacos/">
            
                <a href="../../../../../../Nacos/">
            
                    
                    Nacos
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../../../../../Nacos/Nacos-Introduction.html">
            
                <a href="../../../../../../Nacos/Nacos-Introduction.html">
            
                    
                    Nacos介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../../../../../Nacos/Nacos-Registration.html">
            
                <a href="../../../../../../Nacos/Nacos-Registration.html">
            
                    
                    Nacos注册中心
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                <a href="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                    
                    Nacos+OpenFeign
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../../../../../Sortings/">
            
                <a href="../../../../../../Sortings/">
            
                    
                    排序算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                    
                    希尔排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" >
            
                <span>
            
                    
                    选择排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" >
            
                <span>
            
                    
                    堆排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" >
            
                <span>
            
                    
                    希尔排序
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../../../../.." >访问者模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;">&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;</h3>
<p>&#x751F;&#x6D3B;&#x4E2D;&#xFF0C;&#x6709;&#x5F88;&#x591A;&#x51E0;&#x4F55;&#x5BF9;&#x8C61;&#x5B58;&#x5728;&#x591A;&#x79CD;&#x4E0D;&#x540C;&#x5143;&#x7D20;&#xFF0C;&#x4E14;&#x6BCF;&#x79CD;&#x5143;&#x7D20;&#x4F1A;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x8BBF;&#x95EE;&#x8005;&#x548C;&#x4E0D;&#x540C;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x3002;</p>
<p>eg&#xFF1A;</p>
<p>&#x533B;&#x751F;&#x5F00;&#x7684;&#x5904;&#x65B9;&#x5355;&#xFF0C;&#x67FB;&#x770B;&#x5B83;&#x7684;&#x5212;&#x4EF7;&#x5458;&#x548C;&#x836F;&#x623F;&#x5DE5;&#x4F5C;&#x4EBA;&#x5458;&#x5BF9;&#x5B83;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x4E5F;&#x4E0D;&#x540C;&#xFF0C;&#x5212;&#x4EF7;&#x5458;&#x6839;&#x636E;&#x5904;&#x65B9;&#x5355;&#x4E0A;&#x9762;&#x7684;&#x836F;&#x54C1;&#x540D;&#x548C;&#x6570;&#x91CF;&#x8FDB;&#x884C;&#x5212;&#x4EF7;&#xFF0C;&#x836F;&#x623F;&#x5DE5;&#x4F5C;&#x4EBA;&#x5458;&#x6839;&#x636E;&#x5904;&#x65B9;&#x5355;&#x7684;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x6293;&#x836F;&#x3002;</p>
<p>&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;&#x5C31;&#x662F;&#x5904;&#x7406;&#x8FD9;&#x79CD;&#x573A;&#x666F;&#x7684;&#x3002;</p>
<p>&#x5B9A;&#x4E49;&#xFF1A;&#x5C06;&#x4F5C;&#x7528;&#x4E8E;&#x67D0;&#x79CD;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E2D;&#x7684;&#x5404;&#x79CD;&#x5143;&#x7D20;&#x7684;<code>&#x64CD;&#x4F5C;</code>&#x5206;&#x79BB;&#x51FA;&#x6765;&#x5C01;&#x88C5;&#x6210;<code>&#x72EC;&#x7ACB;&#x7684;&#x7C7B;</code>&#xFF0C;
&#x4F7F;&#x5176;&#x5728;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x524D;&#x63D0;&#x4E0B;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x4F5C;&#x7528;&#x4E8E;&#x8FD9;&#x4E9B;&#x5143;&#x7D20;&#x7684;&#x65B0;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x4E3A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E2D;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x63D0;&#x4F9B;&#x591A;&#x79CD;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#x3002;</p>
<p>&#x5C06;&#x6570;&#x636E;&#x7684;&#x64CD;&#x4F5C;&#x4E0E;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x8FDB;&#x884C;&#x5206;&#x79BB;&#xFF0C;&#x662F;&#x884C;&#x4E3A;&#x7C7B;&#x6A21;&#x5F0F;&#x4E2D;&#x6700;&#x590D;&#x6742;&#x7684;&#x4E00;&#x79CD;</p>
<h3 id="&#x4F18;&#x7F3A;&#x70B9;">&#x4F18;&#x7F3A;&#x70B9;</h3>
<ul>
<li>&#x6269;&#x5C55;&#x6027;&#x597D;&#xFF1A;&#x80FD;&#x591F;&#x5728;&#x4E0D;&#x4FEE;&#x6539;&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x4E2D;&#x5143;&#x7D20;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E3A;&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x4E2D;&#x5143;&#x7D20;&#x6DFB;&#x52A0;&#x65B0;&#x529F;&#x80FD;</li>
<li>&#x590D;&#x7528;&#x6027;&#x597D;&#xFF1A;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8BBF;&#x95EE;&#x8005;&#x6765;&#x5B9A;&#x4E49;&#x6574;&#x4E2A;&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x901A;&#x7528;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x4ECE;&#x800C;&#x63D0;&#x9AD8;&#x7CFB;&#x7EDF;&#x7684;&#x590D;&#x7528;&#x7A0B;&#x5EA6;</li>
<li>&#x7075;&#x6D3B;&#x6027;&#x597D;&#xFF1A;&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;&#x5C06;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E0E;&#x4F5C;&#x7528;&#x4E8E;&#x7ED3;&#x6784;&#x4E0A;&#x7684;&#x64CD;&#x4F5C;&#x89E3;&#x8026;&#x5408;&#xFF0C;&#x4F7F;&#x5F97;&#x64CD;&#x4F5C;&#x96C6;&#x5408;&#x53EF;&#x4EE5;&#x76F8;&#x5BF9;&#x81EA;&#x7531;&#x7684;&#x6F14;&#x5316;&#x800C;&#x4E0D;&#x5F71;&#x54CD;&#x7CFB;&#x7EDF;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;</li>
<li>&#x7B26;&#x5408;&#x5355;&#x4E00;&#x804C;&#x8D23;&#x539F;&#x5219;&#xFF1A;&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;&#x5427;&#x76F8;&#x5173;&#x7684;&#x884C;&#x4E3A;&#x5C01;&#x88C5;&#x5728;&#x4E00;&#x8D77;&#x6784;&#x6210;&#x8BBF;&#x95EE;&#x8005;</li>
</ul>
<p>&#x7F3A;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x589E;&#x52A0;&#x65B0;&#x7684;&#x5143;&#x7D20;&#x4E0D;&#x65B9;&#x4FBF;&#xFF0C;&#x6BCF;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5143;&#x7D20;&#x7C7B;&#xFF0C;&#x90FD;&#x8981;&#x5728;&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;&#x4E2D;&#x589E;&#x52A0;&#x76F8;&#x5E94;&#x5177;&#x4F53;&#x64CD;&#x4F5C;</li>
<li>&#x7834;&#x574F;&#x5C01;&#x88C5;&#xFF0C;&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;&#x4E2D;&#x5177;&#x4F53;&#x5143;&#x7D20;&#x5BF9;&#x8BBF;&#x95EE;&#x8005;&#x516C;&#x5E03;&#x7EC6;&#x8282;</li>
<li>&#x8FDD;&#x53CD;&#x4F9D;&#x8D56;&#x5012;&#x7F6E;&#x539F;&#x5219;&#xFF0C;&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;&#x4F9D;&#x8D56;&#x5177;&#x4F53;&#x7C7B;&#xFF0C;&#x800C;&#x975E;&#x62BD;&#x8C61;&#x7C7B;</li>
</ul>
<h3 id="&#x89D2;&#x8272;&#x548C;&#x5B9E;&#x73B0;">&#x89D2;&#x8272;&#x548C;&#x5B9E;&#x73B0;</h3>
<ul>
<li>&#x62BD;&#x8C61;&#x8BBF;&#x95EE;&#x8005;&#x89D2;&#x8272;&#xFF1A;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x5177;&#x4F53;&#x5143;&#x7D20;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x4E3A;&#x6BCF;&#x4E2A;&#x5177;&#x4F53;&#x5143;&#x7D20;&#x7C7B;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x64CD;&#x4F5C;visit(),&#x8BE5;&#x64CD;&#x4F5C;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x6807;&#x8BC6;&#x4E86;&#x88AB;&#x8BBF;&#x95EE;&#x7684;&#x5177;&#x4F53;&#x5143;&#x7D20;</li>
<li>&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;&#x89D2;&#x8272;&#xFF1A;&#x5B9E;&#x73B0;&#x62BD;&#x8C61;&#x8BBF;&#x95EE;&#x8005;&#x4E2D;&#x7684;&#x8BBF;&#x95EE;&#x64CD;&#x4F5C;&#xFF0C;&#x786E;&#x5B9A;&#x8BBF;&#x95EE;&#x8005;&#x8BBF;&#x95EE;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x8BE5;&#x505A;&#x7684;&#x64CD;&#x4F5C;</li>
<li>&#x62BD;&#x8C61;&#x5143;&#x7D20;&#x89D2;&#x8272;&#xFF1A;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x63A5;&#x6536;&#x64CD;&#x4F5C;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x88AB;&#x63A5;&#x53D7;&#x7684;&#x8BBF;&#x95EE;&#x8005;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;</li>
<li>&#x5177;&#x4F53;&#x5143;&#x7D20;&#x89D2;&#x8272;&#xFF1A;&#x5B9E;&#x73B0;&#x62BD;&#x8C61;&#x5143;&#x7D20;&#x89D2;&#x8272;&#x63D0;&#x4F9B;&#x7684;accept()&#x65B9;&#x6CD5;&#xFF0C;&#x540C;&#x4E8B;&#x5305;&#x542B;&#x4E1A;&#x52A1;&#x672C;&#x8EAB;&#x903B;&#x8F91;</li>
<li>&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x89D2;&#x8272;&#xFF1A;&#x5305;&#x542B;&#x5143;&#x7D20;&#x89D2;&#x8272;&#x7684;&#x5BB9;&#x5668;&#xFF0C;&#x63D0;&#x4F9B;&#x8BA9;&#x8BBF;&#x95EE;&#x8005;&#x5BF9;&#x8C61;&#x904D;&#x5386;&#x5BB9;&#x5668;&#x4E2D;&#x6240;&#x6709;&#x5143;&#x7D20;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x901A;&#x5E38;&#x7531;List&#x3001;Set&#x3001;Map&#x7B49;&#x805A;&#x5408;&#x7C7B;&#x5B9E;&#x73B0;</li>
</ul>
<p><img src="https://raw.githubusercontent.com/larscheng/myImg/master/blogImg/DesignPatterns/20190718134613.png" alt=""></p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> visitor;
<span class="hljs-keyword">import</span> java.util.*;
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">VisitorPattern</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span>
    </span>{
        ObjectStructure os=<span class="hljs-keyword">new</span> ObjectStructure();
        os.add(<span class="hljs-keyword">new</span> ConcreteElementA());
        os.add(<span class="hljs-keyword">new</span> ConcreteElementB());
        Visitor visitor=<span class="hljs-keyword">new</span> ConcreteVisitorA();
        os.accept(visitor);
        System.out.println(<span class="hljs-string">&quot;------------------------&quot;</span>);
        visitor=<span class="hljs-keyword">new</span> ConcreteVisitorB();
        os.accept(visitor);
    }
}
<span class="hljs-comment">//&#x62BD;&#x8C61;&#x8BBF;&#x95EE;&#x8005;</span>
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Visitor</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">visit</span><span class="hljs-params">(ConcreteElementA element)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">visit</span><span class="hljs-params">(ConcreteElementB element)</span></span>;
}
<span class="hljs-comment">//&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;A&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConcreteVisitorA</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Visitor</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">visit</span><span class="hljs-params">(ConcreteElementA element)</span>
    </span>{
        System.out.println(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;A&#x8BBF;&#x95EE;--&gt;&quot;</span>+element.operationA());
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">visit</span><span class="hljs-params">(ConcreteElementB element)</span>
    </span>{
        System.out.println(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;A&#x8BBF;&#x95EE;--&gt;&quot;</span>+element.operationB());
    }
}
<span class="hljs-comment">//&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;B&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConcreteVisitorB</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Visitor</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">visit</span><span class="hljs-params">(ConcreteElementA element)</span>
    </span>{
        System.out.println(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;B&#x8BBF;&#x95EE;--&gt;&quot;</span>+element.operationA());
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">visit</span><span class="hljs-params">(ConcreteElementB element)</span>
    </span>{
        System.out.println(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;B&#x8BBF;&#x95EE;--&gt;&quot;</span>+element.operationB());
    }
}
<span class="hljs-comment">//&#x62BD;&#x8C61;&#x5143;&#x7D20;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Element</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(Visitor visitor)</span></span>;
}
<span class="hljs-comment">//&#x5177;&#x4F53;&#x5143;&#x7D20;A&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConcreteElementA</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Element</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(Visitor visitor)</span>
    </span>{
        visitor.visit(<span class="hljs-keyword">this</span>);
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">operationA</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&#x5177;&#x4F53;&#x5143;&#x7D20;A&#x7684;&#x64CD;&#x4F5C;&#x3002;&quot;</span>;
    }
}
<span class="hljs-comment">//&#x5177;&#x4F53;&#x5143;&#x7D20;B&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConcreteElementB</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Element</span>
</span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(Visitor visitor)</span>
    </span>{
        visitor.visit(<span class="hljs-keyword">this</span>);
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">operationB</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&#x5177;&#x4F53;&#x5143;&#x7D20;B&#x7684;&#x64CD;&#x4F5C;&#x3002;&quot;</span>;
    }
}
<span class="hljs-comment">//&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x89D2;&#x8272;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ObjectStructure</span>
</span>{   
    <span class="hljs-keyword">private</span> List&lt;Element&gt; list=<span class="hljs-keyword">new</span> ArrayList&lt;Element&gt;();   
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(Visitor visitor)</span>
    </span>{
        Iterator&lt;Element&gt; i=list.iterator();
        <span class="hljs-keyword">while</span>(i.hasNext())
        {
            ((Element) i.next()).accept(visitor);
        }      
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(Element element)</span>
    </span>{
        list.add(element);
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(Element element)</span>
    </span>{
        list.remove(element);
    }
}
</code></pre>
<h3 id="&#x573A;&#x666F;&#x6A21;&#x62DF;">&#x573A;&#x666F;&#x6A21;&#x62DF;</h3>
<p>&#x5F20;&#x4E09;&#x901A;&#x8FC7;&#x7535;&#x8111;&#x770B;&#x7535;&#x89C6;&#xFF0C;&#x674E;&#x56DB;&#x901A;&#x8FC7;&#x7535;&#x8111;&#x505A;&#x8F6F;&#x4EF6;&#x5F00;&#x53D1;&#x3002;
&#x5F20;&#x4E09;&#x901A;&#x8FC7;&#x624B;&#x673A;&#x542C;&#x6B4C;&#xFF0C;&#x674E;&#x56DB;&#x901A;&#x8FC7;&#x624B;&#x673A;&#x5B66;&#x82F1;&#x8BED;</p>
<p>&#x4E24;&#x4E2A;&#x4EBA;&#x5BF9;&#x4E8E;&#x7535;&#x8111;&#x3001;&#x624B;&#x673A;&#x8FD9;&#x4E24;&#x4E2A;&#x7269;&#x54C1;&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x4E0D;&#x540C;&#xFF0C;&#x8FBE;&#x5230;&#x7684;&#x7528;&#x9014;&#x4E5F;&#x5C31;&#x4E0D;&#x540C;&#xFF0C;&#x8FD9;&#x4E00;&#x60C5;&#x51B5;&#x7528;&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;&#x63CF;&#x8FF0;</p>
<p>&#x5176;&#x4E2D;&#x89D2;&#x8272;&#x5982;&#x4E0B;</p>
<ul>
<li>&#x62BD;&#x8C61;&#x8BBF;&#x95EE;&#x8005;&#xFF1A;&#x4EBA;&#xFF0C;&#x5B9A;&#x4E49;&#x8BBF;&#x95EE;&#x5177;&#x4F53;&#x5143;&#x7D20;&#x7684;&#x65B9;&#x6CD5;&#xFF08;&#x7535;&#x8111;&#x3001;&#x624B;&#x673A;&#xFF09;</li>
<li>&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;&#xFF1A;&#x5F20;&#x4E09;&#x3001;&#x674E;&#x56DB;&#xFF0C;&#x5B9E;&#x73B0;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#xFF0C;</li>
<li>&#x62BD;&#x8C61;&#x5143;&#x7D20;&#x89D2;&#x8272;&#xFF1A;&#x5DE5;&#x5177;tool&#xFF0C;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x63A5;&#x6536;&#x64CD;&#x4F5C;&#x7684;&#x65B9;&#x6CD5;&#xFF08;&#x63A5;&#x6536;&#x8BBF;&#x95EE;&#x8005;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#xFF09;&#xFF0C;&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x4E3A;&#x5177;&#x4F53;&#x8BBF;&#x95EE;&#x8005;</li>
<li>&#x5177;&#x4F53;&#x5143;&#x7D20;&#x89D2;&#x8272;&#xFF1A;&#x7535;&#x8111;&#x3001;&#x624B;&#x673A;&#xFF0C;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#xFF0C;&#x901A;&#x8FC7;&#x8BBF;&#x95EE;&#x8005;&#x8BBF;&#x95EE;&#x5177;&#x4F53;&#x5143;&#x7D20;&#x4E2D;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x65B9;&#x6CD5;</li>
<li>&#x5BF9;&#x8C61;&#x7ED3;&#x6784;&#x89D2;&#x8272;&#xFF1A;&#x5DE5;&#x5177;&#x96C6;tools</li>
</ul>
<p>&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;
<img src="https://raw.githubusercontent.com/larscheng/myImg/master/blogImg/DesignPatterns/20190718143612.png" alt=""></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../iteratormode/" class="navigation navigation-prev " aria-label="Previous page: 迭代器模式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../mementomode/" class="navigation navigation-next " aria-label="Next page: 备忘录模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"访问者模式","level":"2.1.3.9","depth":3,"next":{"title":"备忘录模式","level":"2.1.3.10","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/mementomode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/mementomode/README.md","articles":[]},"previous":{"title":"迭代器模式","level":"2.1.3.8","depth":3,"path":"Design-Patterns/src/main/java/behavioralmodel/iteratormode/README.md","ref":"Design-Patterns/src/main/java/behavioralmodel/iteratormode/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["expandable-chapters","livereload"],"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Design-Patterns/src/main/java/behavioralmodel/visitormode/README.md","mtime":"2019-07-18T06:36:34.529Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-09T07:20:35.937Z"},"basePath":"../../../../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../../../../gitbook/gitbook.js"></script>
    <script src="../../../../../../gitbook/theme.js"></script>
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

